import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/login'
import Layout from '@/views/layout'

import Home from '@/views/home'
// import Video from '@/views/video'
// import Question from '@/views/question'
// import User from '@/views/user'
// import Search from '@/views/search'
// import Result from '@/views/search/result'
// import Article from '@/views/article'
// import Profile from '@/views/user/profile'
// import Chat from '@/views/user/chat'

// 创建 VueRouter 时，改写push方法
// 为了解决在路由导航守卫中，发生路由跳转时的错误异常
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/',
    name: 'layout',
    component: Layout,
    children: [
      // 元信息就是路由对象中的meta属性，它也是一个对象，它可以自定义一些属性，这些属性可以在使用路由时访问得到。
      { path: '', component: Home, meta: { isKeepAlive: true } }, // 默认显示的子路由
      { path: '/video', component: () => import('../views/video/index.vue') },
      { path: '/question', component: () => import('../views/question/index.vue') },
      { path: '/user', component: () => import('../views/user/index.vue') }
    ]
  },
  // 搜索中心
  {
    path: '/search',
    name: 'search',
    // component: Search
    component: () => import('../views/search/index.vue')
  },
  // 搜索结果页
  {
    path: '/search/result',
    name: 'searchResult',
    // component: Result
    component: () => import('../views/search/result.vue')
  },
  {
    // 以 动态路由 的方式来传入文章编号
    path: '/article/:id',
    name: 'article',
    // component: Article
    component: () => import('../views/article/index.vue')
  },
  {
    path: '/user/profile',
    name: 'profile',
    // component: Profile
    component: () => import('../views/user/profile.vue')
  },
  {
    path: '/user/chat',
    name: 'chat',
    // component: Chat
    component: () => import('../views/user/chat.vue')
  }
]

const router = new VueRouter({
  routes
})

export default router
